package com.jijuxie.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jijuxie.common.core.R;
import com.jijuxie.system.domain.SysMenu;
import com.jijuxie.system.service.ISysMenuService;

/**
 * 菜单权限控制器 处理系统菜单及权限的CRUD操作
 *
 * @author jijuxie
 */
@RestController
@RequestMapping("/system/menu")
public class SysMenuController {

    @Autowired
    private ISysMenuService menuService;

    /**
     * 获取菜单列表 支持按菜单名称、状态查询
     */
    @GetMapping("/list")
    public R<List<SysMenu>> list(SysMenu menu) {
        List<SysMenu> menus = menuService.list(new LambdaQueryWrapper<SysMenu>()
                .like(menu.getMenuName() != null, SysMenu::getMenuName, menu.getMenuName())
                .eq(menu.getStatus() != null, SysMenu::getStatus, menu.getStatus())
                .orderByAsc(SysMenu::getParentId)
                .orderByAsc(SysMenu::getOrderNum));
        return R.ok(menus);
    }

    /**
     * 获取菜单树形结构 用于前端菜单展示
     */
    @GetMapping("/treeselect")
    public R<List<SysMenu>> treeselect() {
        List<SysMenu> menus = menuService.list(new LambdaQueryWrapper<SysMenu>()
                .eq(SysMenu::getStatus, "0")
                .orderByAsc(SysMenu::getParentId)
                .orderByAsc(SysMenu::getOrderNum));
        return R.ok(menuService.buildMenuTree(menus));
    }

    /**
     * 根据菜单编号获取详细信息
     */
    @GetMapping("/{menuId}")
    public R<SysMenu> getInfo(@PathVariable Long menuId) {
        return R.ok(menuService.getById(menuId));
    }

    /**
     * 获取对应角色菜单列表树 用于角色权限分配
     */
    @GetMapping("/roleMenuTreeselect/{roleId}")
    public R<List<SysMenu>> roleMenuTreeselect(@PathVariable Long roleId) {
        List<SysMenu> menus = menuService.selectMenusByRoleId(roleId);
        return R.ok(menuService.buildMenuTree(menus));
    }

    /**
     * 新增菜单
     */
    @PostMapping
    public R<Void> add(@RequestBody SysMenu menu) {
        menuService.save(menu);
        return R.ok();
    }

    /**
     * 修改菜单
     */
    @PutMapping
    public R<Void> edit(@RequestBody SysMenu menu) {
        menuService.updateById(menu);
        return R.ok();
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("/{menuId}")
    public R<Void> remove(@PathVariable Long menuId) {
        if (menuService.count(new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getParentId, menuId)) > 0) {
            return R.fail("存在子菜单,不允许删除");
        }
        menuService.removeById(menuId);
        return R.ok();
    }
}
